home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: OK, so data types must be declared...
- Date: 24 Feb 1996 18:08:17 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb24110817@qcd.lanl.gov>
- References: <4glbau$tge@inet.up.ac.za> <4glfee$185b@inet.up.ac.za>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Rudolph Pienaar's message of 23 Feb 1996 22:34:54 GMT
-
- <snip>
- In article <4glfee$185b@inet.up.ac.za>
- Rudolph Pienaar <rudolph@pangea.ee.up.ac.za> writes:
-
- RP: Thanks for the quick feedback, James -
- RP:
- RP: You wrote:-
- <snip>
- RP: :The only reason that you don't get the warning when the argument type is
- RP: :an int is because for backwards compatibility reasons, (*func)()
- is allowed
- RP: :to be treated as (*func)(int).
-
- This answer is wrong!!! (*func)() is _not_ (*func)(int). I explained
- in my other post in this thread.
-
- RP:
- RP: Of course... You're completely right. However, I purposefully
- didn't want to
- RP: explicitly associate the passed function to any one data type.
-
- For you to be able to _call_ the function, you need to know the data
- type to call it with. When you call `passed(1)', how does the compiler
- know what form of 1 you want to pass? char, unsigned char, signed
- char, short, unsigned short, int, unsigned int, long, unsigned long,
- float, double or long double? If you write (), the rule it uses is
- simple: if you write 1, it passes int, 1L, it passes long and so
- on. In fact anything before int in my list is passed as int or
- unsigned int, the exact rules are called integral promotion; and
- floats are pased as doubles: the rest are passed exactly as you write
- it. So, you see that even if you declare it (), _you_ need to know
- exactly what it wants. And, if you know it, most often you can declare
- it correctly as well. (There are exceptions when () is useful. They
- are rare.)
-
- In any case, if you really want (), then the actual function cannot
- be prototyped with parameters of type float, or anything shorter than
- int or with enum.
-
- RP:
- RP: Basically I could have a function called "Matrix Dfsys_eval(Dfsysdatatype
- RP: *sys)" and another one called "Matrix PIsys_eval(PIsysdatatype
- *sys)" (where
- RP: Matrix is some user defined matrix data type, likewise for the
- different *sys).
- RP: Here, the "sys" variable is a struct that contains data specific
- to one or the
- RP: other system, and each "eval" knows how to combine this data to generate
- RP: output. The integration routine was declared "Matrix RK4(Matrix
- (*F)())" and
- RP: was called by either:
- RP:
- RP: RK4(Dfsys_eval) or RK4(PIsys_eval)...
- RP:
- RP: In its body, it simply called a " (*F)(&sys);" to get the returned
- value of any
- RP: system type.
-
- How does it get the sys? Are you guaranteed that it has the correct
- data type for the routine called? If you are, then your declaration is
- correct and everything should work fine. (Remember pointers don't get
- promoted when calling, so if sys is the correct type, &sys is fine in
- the call.)
-
- Of course, if you want to pass a null pointer, there is no way to do
- it without _knowing_ the type.
-
- RP:
- RP: The only way I could think around this was to declare each eval like
- RP: "Matrix Dfsys_eval(void *sys)", and making sure of being
- consistent throughout.
- RP: Of course though, this doesn't help because the "sys" in
- "Dfsysdatatype *sys"
- RP: contains data specific to the df system, and with void pointers
- this data can't
- RP: be deferenced; moreover trying to dereference a void pointer also
- generates a
- RP: host of warnings.
- RP:
-
- That way is no more safe, and is just obfuscating. If you really want
- to do it that way, don't dereference a void poiner. Inside the
- routine, declare a local variable `Dfsysdatatype realsys = sys', and
- use realsys instead. (It does allow you to pass a null pointer,
- however; and that is the only reason I comment on how to do it
- right. Another, and possibly slightly better, possibility is to
- replace the void* with a pointer to and appropriate union type. This
- needs a cast in the realsys declaration line however.)
-
- RP:
- RP: I hope that you could glean an inkling of what I'm getting at. Any
- suggestions?
- RP:
-
- It would help if you gave a realistic short example instead of getting
- confused over floats etc.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-